const getDefaultSplitChunksConfig = () => {
  return {
    cacheGroups: {
      // commons: {
      //   test: /[\\/]node_modules[\\/]/,
      //   name: 'vendors',
      //   chunks: 'all',
      // },
      importantstyles: {
        name: 'importantstyles',
        test: /.+\.important\.less$/,
        chunks: 'all',
        priority: -10,
      },
      styles: {
        name: 'styles',
        test: /(?<!\.important)\.less$/,
        chunks: 'all',
        priority: 10,
      },
    },
  };
};

const getSplitChunksConfig = ({ chunks } = {}) => {
  const paths = require('../paths');

  const cpaasConfig = paths.getCpaasBootConfig(paths.appPath) || {};
  let config = {};
  if (cpaasConfig.splitChunks) {
    config = cpaasConfig.splitChunks;
  } else {
    config = getDefaultSplitChunksConfig();
  }

  if (chunks) {
    if (config.chunks) {
      config.chunks = chunks;
    }
    if (config.cacheGroups) {
      Object.values(config.cacheGroups).forEach((item) => {
        if (item.chunks) {
          item.chunks = chunks;
        }
      });
    }
  }

  return config;
};

module.exports = getSplitChunksConfig;
